<?php
namespace yan\soundcaptcha;
use Yii;
use yii\web\Response;
/**
 * public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4,
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
            'sound' => [
                'class' => '\yan\soundcaptcha\SoundAction',
                'captchaAction' => 'site/captcha',
            ],
        ];
    }
 * 
 * */
class SoundAction extends \yii\base\Action{
	public $captchaAction = 'site/captcha';
	public function run(){
		$v = Yii::$app->request->get('v');
		if($v!= 0 && empty($v)){
			throw new \yii\web\NotFoundHttpException("Error Processing Request");
		}
		$captcha = $this->createCaptchaAction();
		$code = $captcha->getVerifyCode(false);
		$name = isset($code[(int)$v]) ? $code[(int)$v] : null;
		if(empty($name)){
			throw new \yii\web\NotFoundHttpException("Error Processing Request");
		}
		$file_name = strtoupper($name).'.wav';
		$filePath = __DIR__. '/assets/audio/'.$file_name;
		return Yii::$app->response->sendFile($filePath, time().'.wav');
	}
	public function createCaptchaAction()
    {
        $ca = Yii::$app->createController($this->captchaAction);
        if ($ca !== false) {
            /* @var $controller \yii\base\Controller */
            list($controller, $actionID) = $ca;
            $action = $controller->createAction($actionID);
            if ($action !== null) {
                return $action;
            }
        }
        throw new InvalidConfigException('Invalid CAPTCHA action ID: ' . $this->captchaAction);
    }
}